using System;
using System.Collections;
using Castle.Windsor;
using Microsoft.Practices.ServiceLocation;

namespace XWord.Common
{
    public class EntityFactory : IEntityFactory
    {
        private readonly IWindsorContainer _serviceLocator;

        public EntityFactory(IWindsorContainer serviceLocator)
        {
            _serviceLocator = serviceLocator;
        }

        public T Create<T>()
        {
            try
            {
                return _serviceLocator.Resolve<T>();
            }
            catch (ActivationException)
            {
                return Activator.CreateInstance<T>();
            }

        }

        public T Create<T>(IDictionary args)
        {
            try
            {
                return _serviceLocator.Resolve<T>(args);
            }
            catch (ActivationException)
            {
                return Activator.CreateInstance<T>();
            }
        }
    }
}